/*
 * Created by JFormDesigner on Mon Aug 28 20:46:42 CST 2023
 */

package src;

import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
import javax.swing.*;

/**
 * @author Lenovo
 */
public class lookPage extends JFrame {
    public void clean(){

        this.textFieldName.setText("");
        this.radioButtonMan.setSelected(false);
        this.radioButtonWomen.setSelected(false);
        this.textFieldPro.setText("");
        this.textFieldGrade.setText("");
        this.textFieldBorn.setText("");

    }

    public lookPage() {
        initComponents();
    }

    private void buttonBackMouseClicked(MouseEvent e) {
        this.dispose();
        new mainPage().setVisible(true);
    }

    private void buttonFindMouseClicked(MouseEvent e) throws IOException {
        this.labelError.setText("");
        String str = this.textFieldNumber.getText();
        //使用str字段查找到对应学生，将内容映射到各个文本框内
        StudentData sd = new StudentData();
        int len = sd.getFileDate();
        int i = sd.findData(sd.stdsdata, len,str);
        if(i>=0){
            this.textFieldName.setText(sd.stdsdata[i].getName());
            String sex = sd.stdsdata[i].getSex();
            if(sex.equals("男")){
                this.radioButtonMan.setSelected(true);
                this.radioButtonWomen.setSelected(false);
            }
            if(sex.equals("女")){
                this.radioButtonMan.setSelected(false);
                this.radioButtonWomen.setSelected(true);

            }
            this.textFieldPro.setText(sd.stdsdata[i].getPro());
            this.textFieldGrade.setText(sd.stdsdata[i].getGrade());
            this.textFieldBorn.setText(sd.stdsdata[i].getBorn());


        }else{
            clean();
            this.labelError.setText("查无此人，请检查输入是否正确！");
        }


    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        label1 = new JLabel();
        label2 = new JLabel();
        label3 = new JLabel();
        label4 = new JLabel();
        label5 = new JLabel();
        textFieldNumber = new JTextField();
        textFieldName = new JTextField();
        radioButtonMan = new JRadioButton();
        radioButtonWomen = new JRadioButton();
        label6 = new JLabel();
        textFieldPro = new JTextField();
        textFieldGrade = new JTextField();
        textFieldBorn = new JTextField();
        buttonFind = new JButton();
        buttonBack = new JButton();
        labelError = new JLabel();

        //======== this ========
        setTitle("\u67e5\u8be2\u7a97\u53e3");
        var contentPane = getContentPane();
        contentPane.setLayout(null);

        //---- label1 ----
        label1.setText("\u8f93\u5165\u8981\u67e5\u8be2\u7684\u5b66\u53f7\uff1a");
        contentPane.add(label1);
        label1.setBounds(10, 10, 115, 25);

        //---- label2 ----
        label2.setText("\u59d3\u540d\uff1a");
        contentPane.add(label2);
        label2.setBounds(new Rectangle(new Point(20, 45), label2.getPreferredSize()));

        //---- label3 ----
        label3.setText("\u6027\u522b\uff1a");
        contentPane.add(label3);
        label3.setBounds(20, 85, 45, label3.getPreferredSize().height);

        //---- label4 ----
        label4.setText("\u4e13\u4e1a\uff1a");
        contentPane.add(label4);
        label4.setBounds(new Rectangle(new Point(20, 125), label4.getPreferredSize()));

        //---- label5 ----
        label5.setText("\u51fa\u751f\uff1a");
        contentPane.add(label5);
        label5.setBounds(new Rectangle(new Point(20, 185), label5.getPreferredSize()));
        contentPane.add(textFieldNumber);
        textFieldNumber.setBounds(130, 5, 195, textFieldNumber.getPreferredSize().height);
        contentPane.add(textFieldName);
        textFieldName.setBounds(60, 40, 215, textFieldName.getPreferredSize().height);

        //---- radioButtonMan ----
        radioButtonMan.setText("\u7537");
        contentPane.add(radioButtonMan);
        radioButtonMan.setBounds(new Rectangle(new Point(80, 85), radioButtonMan.getPreferredSize()));

        //---- radioButtonWomen ----
        radioButtonWomen.setText("\u5973");
        contentPane.add(radioButtonWomen);
        radioButtonWomen.setBounds(new Rectangle(new Point(140, 85), radioButtonWomen.getPreferredSize()));

        //---- label6 ----
        label6.setText("\u5e74\u7ea7\uff1a");
        contentPane.add(label6);
        label6.setBounds(new Rectangle(new Point(20, 155), label6.getPreferredSize()));
        contentPane.add(textFieldPro);
        textFieldPro.setBounds(60, 120, 215, 30);
        contentPane.add(textFieldGrade);
        textFieldGrade.setBounds(60, 150, 215, 30);
        contentPane.add(textFieldBorn);
        textFieldBorn.setBounds(60, 180, 215, 30);

        //---- buttonFind ----
        buttonFind.setText("\u67e5\u8be2");
        buttonFind.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                try {
                    buttonFindMouseClicked(e);
                } catch (IOException ex) {
                    throw new RuntimeException(ex);
                }
            }
        });
        contentPane.add(buttonFind);
        buttonFind.setBounds(325, 5, 70, buttonFind.getPreferredSize().height);

        //---- buttonBack ----
        buttonBack.setText("\u8fd4\u56de");
        buttonBack.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                buttonBackMouseClicked(e);
            }
        });
        contentPane.add(buttonBack);
        buttonBack.setBounds(new Rectangle(new Point(285, 225), buttonBack.getPreferredSize()));
        contentPane.add(labelError);
        labelError.setBounds(25, 225, 230, 30);

        {
            // compute preferred size
            Dimension preferredSize = new Dimension();
            for(int i = 0; i < contentPane.getComponentCount(); i++) {
                Rectangle bounds = contentPane.getComponent(i).getBounds();
                preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
            }
            Insets insets = contentPane.getInsets();
            preferredSize.width += insets.right;
            preferredSize.height += insets.bottom;
            contentPane.setMinimumSize(preferredSize);
            contentPane.setPreferredSize(preferredSize);
        }
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    private JLabel label1;
    private JLabel label2;
    private JLabel label3;
    private JLabel label4;
    private JLabel label5;
    private JTextField textFieldNumber;
    private JTextField textFieldName;
    private JRadioButton radioButtonMan;
    private JRadioButton radioButtonWomen;
    private JLabel label6;
    private JTextField textFieldPro;
    private JTextField textFieldGrade;
    private JTextField textFieldBorn;
    private JButton buttonFind;
    private JButton buttonBack;
    private JLabel labelError;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
